package cn.com.surker.common;

import cn.com.surker.handler.PrintHandler;
import cn.com.surker.production.entity.ProductionInvoice;
import cn.com.surker.production.entity.vo.ProductionInvoiceVo;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.util.DateUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @author luotao
 * @date 2025/1/2
 */
@RestController
@RequestMapping("/common/api")
public class ApiController extends AbstractController {


	@Autowired
	private PrintHandler printHandler;

	/**
	 * 查询最最新的一条打印
	 * @param    type        打印类型:0为普通打印;1为过磅打印
	 * @return
	 */
	@RequestMapping(value = "/print/inv", method = RequestMethod.GET)
	@ResponseBody
	public RestResultDto printData(Integer type) {
		String printKey = PrintHandler.disPrintKey;
		if (type != null && type == 1) {
			//	过磅打印
			printKey = PrintHandler.weightPrintKey;
		}
		//	查询数据
		Object rVo = redisUtils.leftPop(printKey);
		if (rVo == null) {
			//	没有数据
			return RestResultDto.fail();
		}
		ProductionInvoiceVo invoiceVo = (ProductionInvoiceVo) rVo;
		//	转换数据
		PrintInvoiceData printInvoiceData = new PrintInvoiceData(orgFullName, invoiceVo);
		return RestResultDto.success(printInvoiceData);
	}

	/**
	 * 将发货单数据传入到缓存
	 *
	 * @param invId
	 * @return
	 */
	@RequestMapping(value = "/print/add/inv", method = RequestMethod.GET)
	@ResponseBody
	public RestResultDto addInv(Long invId) {
		if (invId == null) {
			return RestResultDto.fail(Code.PARAM_EMPTY);
		}
		String indentDispathcKey = PrintHandler.weightPrintKey + "_" + invId;
		//	加锁,一个发货单5秒钟1次
		boolean acquire = redisUtils.acquireLock(indentDispathcKey, 5);
		if (!acquire) {
			//	没有加锁成功,不执行
			return RestResultDto.fail("操作过快");
		}
		//	判断队列中是否存在
		List<Object> oldDatas = redisUtils.listValues(PrintHandler.weightPrintKey);
		if (oldDatas != null && oldDatas.size() > 0) {
			//	判断是否存在数据
			for (Object obj : oldDatas) {
				ProductionInvoice inv = (ProductionInvoice) obj;
				Long id = inv.getId();
				if (invId.equals(id)) {
					//	有过数据,不操作了
					return RestResultDto.success();
				}
			}
		}
		printHandler.addInvoice(PrintHandler.weightPrintKey, invId);
		return RestResultDto.success();
	}

}

@Data
class PrintInvoiceData {

	public PrintInvoiceData(String orgFullName, ProductionInvoiceVo invoiceVo) {
		this.receiptUnit = orgFullName;
		this.conName = invoiceVo.getConName();
		String clientName = invoiceVo.getClientName();
		if (clientName == null) {
			throw new MessageCenterException("暂无客户名称,请同步客户信息到erp");
		}
		//	判断施工单位和客户名称是否一致
		String builder = invoiceVo.getBuilder();
		if (!clientName.equals(builder)) {
			//	不一致
			clientName = clientName + " " + builder;
		}
		this.clientName = clientName;
		this.site = invoiceVo.getSite();
		//	强度等级
		this.strengthName = invoiceVo.getPrintStrength();
		this.tech = invoiceVo.getTech();
		this.castModelName = invoiceVo.getCastModelName();
		this.foreman = "";
		this.pumpDriver = invoiceVo.getPumpDriver();
		this.slumps = invoiceVo.getSlumps();
		this.driver = invoiceVo.getDriver();
		this.carCode = invoiceVo.getCarCode();
		this.carNo = invoiceVo.getCarNo();
		this.sendStere = String.valueOf(invoiceVo.getSendStere());
		this.totalCar = String.valueOf(invoiceVo.getTotalCar());
		this.totalStere = String.valueOf(invoiceVo.getTotalStere());
		this.indentCode = invoiceVo.getIndentCode();
		this.code = invoiceVo.getCode();
		@NotNull(message = "请选择生产完成时间") Date proTime = invoiceVo.getProTime();
		if (proTime == null) {
			proTime = invoiceVo.getCreateTime();
		}
		this.proTime = DateUtil.convertString(proTime);
		this.remark = invoiceVo.getRemark();
		this.dispatcher = invoiceVo.getDispatcherName();
		//	泵送设备
		String pumpDeviceName = invoiceVo.getPumpDeviceName();
		this.pumpDevice = pumpDeviceName;
		this.pumpTeamCode = invoiceVo.getPumpTeamCode();
		this.id = invoiceVo.getId();
		//	
		this.buildPhone = invoiceVo.getLinkPhone();
	}

	private Long id;

	private String receiptUnit;

	private String conName;

	private String clientName;

	private String site;

	private String strengthName;

	private String tech;

	private String castModelName;

	private String foreman;

	private String pumpDriver;

	private String slumps;

	private String driver;

	private String carCode;

	private String carNo;

	private String sendStere;

	private String totalCar;

	private String totalStere;

	private String indentCode;

	private String code;

	private String proTime;

	private String remark;

	private String dispatcher;

	private String pumpDevice;

	/**
	 * 泵车对编码
	 */
	private String pumpTeamCode;

	/**
	 * 施工方电话号码
	 */
	private String buildPhone;

}